package au.com.twosquared.animalfarm;

import java.util.ArrayList;

import au.com.twosquared.animalfarm.entities.EntityList;
import au.com.twosquared.animalfarm.entities.animals.bird.Chicken;
import au.com.twosquared.animalfarm.map.Map;
import au.com.twosquared.animalfarm.sprites.Animation;
import au.com.twosquared.animalfarm.sprites.Image;
import au.com.twosquared.tools.Random;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

/**
 *Draws all the game elements including the screen
 * 
 * @author Jan Martin <jan.martin@twosquared.com.au>
 * @version 1.0 2011-15-11
 * @since 2011-15-11
 *
 */

public class Game implements ApplicationListener {

	/**
	 * The width of the screen
	 */
	int screenWidth;
	/**
	 * The height of the screen
	 */
	int screenHeight;
	
	/**
	 * The sprites
	 */
	SpriteBatch batch;
	/**
	 * The map
	 */
	Map map;
	/**
	 * All entities
	 */
	EntityList entities = new EntityList();
	/**
	 * Timing
	 */
	long delta;
	long time;
	
	/**
	 * Defines the screen dimensions, sprites and map based on the width and height of the screen
	 */
	public void create() {
		screenWidth = Gdx.graphics.getWidth();
		screenHeight = Gdx.graphics.getHeight();
		// Cornflower blue
		Gdx.gl.glClearColor(146f / 255 , 201f / 255, 239f / 255, 1);

		batch = new SpriteBatch();
		
		map = new Map(screenWidth, screenHeight);
		map.loadTileset();
		
		// Load images
		Image[] right = Image.load("sprites/Chicken/chickenR.png", 2, 48, 48);
		Image[] left = Image.load("sprites/Chicken/chickenL.png", 2, 48, 48);
		Image[] idleStillRight = Image.load("sprites/Chicken/chickenSR.png", 14, 48, 48);
		Image[] idleStillLeft = Image.load("sprites/Chicken/chickenSL.png", 14, 48, 48);
		Image[] runRight = Image.load("sprites/Chicken/chickenRR.png", 6, 48, 48);
		Image[] runLeft = Image.load("sprites/Chicken/chickenRL.png", 6, 48, 48);
		Image[] idleRight = Image.load("sprites/Chicken/chickenIR.png", 20, 48, 48);
		Image[] idleLeft = Image.load("sprites/Chicken/chickenIL.png", 20, 48, 48);
		
		for(int i = 0; i < 1; i++) {
			ArrayList<Animation> chickenAnimations = new ArrayList<Animation>();
			
			// Convert image strips into animations
			Animation idleSRAnimation = new Animation(idleStillRight, 100);
			Animation idleSLAnimation = new Animation(idleStillLeft, 100);
			Animation runRAnimation = new Animation(runRight, 100);
			Animation runLAnimation = new Animation(runLeft, 100);
			Animation idleRAnimation = new Animation(idleRight, 100);
			Animation idleLAnimation = new Animation(idleLeft, 100);
			Animation rightAnimation = new Animation(right, 500);
			Animation leftAnimation = new Animation(left, 500);
			
			// Add animations to the entity
			chickenAnimations.add(rightAnimation);
			chickenAnimations.add(idleSRAnimation);
			chickenAnimations.add(idleRAnimation);
			chickenAnimations.add(leftAnimation);
			chickenAnimations.add(idleSLAnimation);
			chickenAnimations.add(idleLAnimation);
			chickenAnimations.add(runRAnimation);
			chickenAnimations.add(runLAnimation);
			
			Chicken chicken = new Chicken(chickenAnimations);
			chicken.x = Random.integer(96, screenWidth - 96);
			chicken.y = 4 * 48;
			entities.add(chicken);
		}
		
		time = System.currentTimeMillis();
	}

	public void update() {
		delta = System.currentTimeMillis() - time;
		time = System.currentTimeMillis();
		
		entities.update(delta);
		entities.collision(map);
	}
	
	/**
	 * Renders the map and graphics
	 */
	public void render() {
		update();
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		batch.begin();
		map.draw(batch);
		entities.draw(batch);
		batch.end();
	}
	
	/**
	 * Resizes the screen given new dimensions
	 * @param width the new width
	 * @param height the new height
	 */
	public void resize(int width, int height) {
		this.screenWidth = width;
		this.screenHeight = height;
	}

	public void pause() {

	}

	public void resume() {

	}

	public void dispose() {

	}
}